package uk.ac.cloudcomputing;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import org.apache.commons.collections.Predicate;


public class NotesManager implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -4513701641955379412L;
	private List<Note>				list = null;
	private ArrayList<SelectItem>	test = null;
	private String					choice = "";
	private Note			n;

	public NotesManager() {

		if (this.list == null) {
			System.out.println("CALL CONSTRUCTOR");
/*			this.list = new ArrayList<Note>();

			this.list.add(new Note(1, "tata@test.com", "tata"));
			this.list.add(new Note(2, "tete@test.com", "tete"));
			this.list.add(new Note(3, "titi@test.com", "titi"));
			this.list.add(new Note(4, "toto@test.com", "toto"));
			this.list.add(new Note(5, "tutu@test.com", "tutu"));
			this.list.add(new Note(6, "tyty@test.com", "tyty"));*/
			this.list = uk.ac.cloudcomputing.database.Note.getNotes();
			uk.ac.cloudcomputing.database.Note.persist(new Note(this.list.size(), "note" + this.list.size(), "n"));
			this.list = uk.ac.cloudcomputing.database.Note.getNotes();
			for (Note note : this.list) {
				System.out.println("Note:" + note.getEmail());
			}
			this.n = new Note();
		}
		
		if (test == null) {
			test = new ArrayList<SelectItem>();
			SelectItem option = new SelectItem("1", "choice1");
			test.add(option);
			option = new SelectItem("2", "choice2");
			test.add(option);
			option = new SelectItem("3", "choice3");
			test.add(option);
			option = new SelectItem("4", "choice4");
			test.add(option);
		}
			
	}

	public void submit() {
		System.out.println("Id received:" + this.choice);
	}


	public Note getMatchingNote(Predicate predicate)
	{
		for (Note temp_n : this.list)
			if (predicate.evaluate(temp_n)) {
				return temp_n;
			}
		return null;
	}

	// Setters

	public void setList(ArrayList<Note> list) {
		this.list = list;
	}

	// Getters

	public List<Note> getList() {
		return this.list;
	}

	public ArrayList<SelectItem> getTest() {
		return test;
	}

	public void setTest(ArrayList<SelectItem> test) {
		this.test = test;
	}

	public String getChoice() {
		return choice;
	}

	public void setChoice(String choice) {
		this.choice = choice;
	}

}